package io.zyw.okhttpx;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import java.io.IOException;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity {

  @Override protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    findViewById(R.id.test_btn).setOnClickListener(new View.OnClickListener() {
      @Override public void onClick(View view) {
        OkHttpClient okHttpClient = new OkHttpClient();
        final Request build = new Request.Builder().get().url("http://baidu.com").build();
        okHttpClient.newCall(build).enqueue(new Callback() {
          @Override public void onFailure(Call call, IOException e) {
            Log.e("http",e.getMessage());
          }

          @Override public void onResponse(Call call, Response response) throws IOException {
            Log.e("http",response.body().string());
          }
        });
      }
    });
  }
}
